package com.sansan.qiangji.schedule;

import com.sansan.qiangji.entity.device.Belt;
import com.sansan.qiangji.entity.device.Scan;
import com.sansan.qiangji.entity.device.Stacker;
import com.sansan.qiangji.realize.early.EarlyWarningReal;
import com.sansan.qiangji.service.impl.device.BeltServiceImpl;
import com.sansan.qiangji.service.impl.device.ScanServiceImpl;
import com.sansan.qiangji.service.impl.device.StackerServiceImpl;
import com.sansan.qiangji.task.AsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

//@Component
@Slf4j
public class Schedule {
    private StackerServiceImpl stackerService;
    private BeltServiceImpl beltService;
    private AsyncTask asyncTask;
    private EarlyWarningReal earlyreal;
    private ScanServiceImpl scanService;

    @Resource
    public void setStackerService(StackerServiceImpl stackerService) {
        this.stackerService = stackerService;
    }

    @Resource
    public void setAsyncTask(AsyncTask asyncTask) {
        this.asyncTask = asyncTask;
    }

    @Resource
    public void setBeltService(BeltServiceImpl beltService) {
        this.beltService = beltService;
    }

    @Resource
    public void setEarlyreal(EarlyWarningReal earlyreal) {
        this.earlyreal = earlyreal;
    }

    @Resource
    public void setScanService(ScanServiceImpl scanService) {
        this.scanService = scanService;
    }

    @Scheduled(fixedDelay = 2000)
    public void receiveScanData() {
        List<Scan> scans = scanService.findAllScan();
        List<Future<Boolean>> futures = new ArrayList<>();
        for (Scan scan : scans) {
            Future<Boolean> future = asyncTask.receiveScanCode(scan);
            futures.add(future);
        }
        while (true) {
            boolean isAllDone = true;
            for (Future<Boolean> future : futures) {
                if (!future.isDone()) {
                    isAllDone = false;
                }
            }
            if (isAllDone) {
                break;
            }
            try {
                Thread.sleep(200);
            }catch (Exception ignored){
            }
        }
    }

    //@Scheduled(fixedDelay = 1000)
    public void updateStackerStatus() {
        List<Stacker> stackers = stackerService.findAllStacker();
        List<Future<Boolean>> futures = new ArrayList<>();
        for (Stacker stacker : stackers) {
            Future<Boolean> future = asyncTask.checkStackerStatus(stacker);
            futures.add(future);
        }
        while (true) {
            boolean isAllDone = true;
            for (Future<Boolean> future : futures) {
                if (!future.isDone()) {
                    isAllDone = false;
                }
            }
            if (isAllDone) {
                break;
            }
            try {
                Thread.sleep(200);
            }catch (Exception ignored){
            }
        }
    }

    //@Scheduled(fixedDelay = 1000)
    public void updateBeltStatus() {
        List<Belt> belts = beltService.findAllBelt();
        List<Future<Boolean>> futures = new ArrayList<>();
        for (Belt belt : belts) {
            Future<Boolean> future = asyncTask.checkBeltStatus(belt);
            futures.add(future);
        }
        while (true) {
            boolean isAllDone = true;
            for (Future<Boolean> future : futures) {
                if (!future.isDone()) {
                    isAllDone = false;
                }
            }
            if (isAllDone) {
                break;
            }
            try {
                Thread.sleep(200);
            }catch (Exception ignored){
            }
        }
    }

    //@Scheduled(fixedDelay = 1000)
    public void writeStackerParamDetail() {
        List<Stacker> stackers = stackerService.findAllStacker();
        List<Future<Boolean>> futures = new ArrayList<>();
        for (Stacker stacker : stackers) {
            Future<Boolean> future = asyncTask.writeStackerParamDetail(stacker);
            futures.add(future);
        }
        while (true) {
            boolean isAllDone = true;
            for (Future<Boolean> future : futures) {
                if (!future.isDone()) {
                    isAllDone = false;
                }
            }
            if (isAllDone) {
                break;
            }
            try {
                Thread.sleep(200);
            }catch (Exception ignored){
            }
        }
    }

    //@Scheduled(fixedDelay = 1000)
    public void writeBeltParamDetail() {
        List<Belt> belts = beltService.findAllBelt();
        List<Future<Boolean>> futures = new ArrayList<>();
        for (Belt belt : belts) {
            Future<Boolean> future = asyncTask.writeBeltParamDetail(belt);
            futures.add(future);
        }
        while (true) {
            boolean isAllDone = true;
            for (Future<Boolean> future : futures) {
                if (!future.isDone()) {
                    isAllDone = false;
                }
            }
            if (isAllDone) {
                break;
            }
            try {
                Thread.sleep(200);
            }catch (Exception ignored){
            }
        }
    }

    /**
     * 每隔30分钟刷一次库存上下限
     */
    @Scheduled(cron = "0 */30 * * * ?")
    //@Scheduled(cron="*/5 * * * * ?")
    public void EarlyStockList(){
        earlyreal.getEarlyStockList();
    }

    /**
     * 凌晨1点刷新超期天数
     */
    @Scheduled(cron = "0 0 1 * * ?")
    //@Scheduled(cron="*/5 * * * * ?")
    public void EarlyOverdueList(){
        earlyreal.getEarlyOverdueList();
    }
}
